class Computer < ActiveRecord::Base
	belongs_to :room
	belongs_to :softwares
	has_many :events, :dependent => :delete_all
	
	validates_numericality_of :cpu
	validates_numericality_of :ram
	validates_numericality_of :hard_drive
	
	validates_numericality_of :cpu_used
	validates_numericality_of :ram_used
	validates_numericality_of :hard_drive_used
	
	validates_associated :room
	validates_presence_of :software_id
	
	validates_presence_of :mac_adress
	validates_presence_of :ip_computer
	validates_presence_of :name
	
	validates_format_of :ip_computer, :with => /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/, :message => "l'adresse IP est invalide"
	validates_format_of :mac_adress, :with => /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/, :message => "l'adresse MAC est invalide"

# simulation de l'agent
#	enregistre des pourcentage d'utilisation aleatoire de la ram, cpu, et dd
# appelé dans cron.rb
	def self.agent
	 
	  
	    @rand_record = self.find(:first, :order => "RAND()")
      antivirus = Softwares.find_by_id(@rand_record.software_id)
      expiration = Time.now.to_datetime + 1.week
     
	  if antivirus.end_licence <=  expiration
	    event_tab = { "information" => " la licence de l'antivirus arrive a terme",
              "state" => 1,
              "computer_id" => @rand_record.id,
              "type_event_id" => 2
            }
      event = Event.new(event_tab)

      if event.save
        puts "date expiration antivirus"
      end
	  end
    @cpu_used = (1 + rand(10)) * 10
    @ram_used = (1 + rand(10)) * 10
    @hard_drive_used = (1 + rand(10)) * 10
    
    
    if  @hard_drive_used >= 70
      type_event = 1
      message = "Incident matériel : capacité disque dur à #{@hard_drive_used}%"
    elsif @hard_drive_used >= 80
      type_event = 2
      message = "Incident matériel : capacité disque dur à #{@hard_drive_used}%"
    elsif @hard_drive_used >= 90
      type_event = 3
      message = "Incident matériel : capacité disque dur à #{@hard_drive_used}%"
    end
     
     
     if  @ram_used >= 70
      type_event = 1
      message = "Incident matériel : utilisation de la ram à #{@ram_used}%"
    elsif @ram_used >= 80
      type_event = 2
      message = "Incident matériel : utilisation de la ram à #{@ram_used}%"
    elsif @ram_used >= 90
      type_event = 3
      message = "Incident matériel : utilisation de la ram à  #{@ram_used}%"
    end
    
    if  @cpu_used >= 100
      type_event = 2
      message = "Incident matériel : utilisation du cpu à #{@cpu_used}%"
    
    end
      # add event
      if type_event
      event_tab = { "information" => message,
              "state" => 1,
              "computer_id" => @rand_record.id,
              "type_event_id" => type_event
            }
      event = Event.new(event_tab)

      if event.save
       puts "Un nouvelle incident a été détecté"
      #flash[:incident]= "Un nouvelle incident a été détecté"
      end
     
    end
    
    record_tab = { "cpu" => @cpu_used,
                   "ram_used" => @ram_used,
                   "hard_drive_used" => @hard_drive_used
                 }
    @rand_record.update_attributes(record_tab)
	  
	end
	
	
end
